package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.mapper.CityInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    ApartmentFacilityService apartmentFacilityService;

    @Autowired
    ApartmentLabelService apartmentLabelService;

    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    GraphInfoService graphInfoService;

    @Autowired
    ProvinceInfoService provinceInfoService;

    @Autowired
    CityInfoService cityInfoService;

    @Autowired
    DistrictInfoService districtInfoService;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;


    /**
     * 判断是保存还是更新
     * 如果是更新,删除公寓配套信息,删除公寓标签,删除公寓杂费,删除公寓图片重新添加
     * 如果是保存,将信息添加到公寓表,公寓配套表,公寓标签表,公寓杂费表,图片表
     */
    @Override
    public void isaveOrUpdate(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();
        if (!Objects.isNull(id)){
            //删除公寓配套信息
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
            //删除公寓标签信息
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
            //删除公寓杂费信息
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
            //删除公寓图片信息
            LambdaQueryWrapper<GraphInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            queryWrapper.eq(GraphInfo::getItemId,id);
            graphInfoService.remove(queryWrapper);
        }
        //设置省份名称
        Long provinceId = apartmentSubmitVo.getProvinceId();
        String provinceName = provinceInfoService.getById(provinceId).getName();
        apartmentSubmitVo.setProvinceName(provinceName);

        //设置城市名称
        Long cityId = apartmentSubmitVo.getCityId();
        String cityName = cityInfoService.getById(cityId).getName();
        apartmentSubmitVo.setCityName(cityName);

        //设置区县名称
        Long districtId = apartmentSubmitVo.getDistrictId();
        String districtName = districtInfoService.getById(districtId).getName();
        apartmentSubmitVo.setDistrictName(districtName);

        //保存公寓信息
        super.saveOrUpdate(apartmentSubmitVo);

        //回填公寓id
        id = apartmentSubmitVo.getId();

        //保存公寓配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(id).facilityId(facilityInfoId).build();
            apartmentFacilities.add(apartmentFacility);
        }
        apartmentFacilityService.saveBatch(apartmentFacilities);

        //保存公寓标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        for (Long labelId : labelIds) {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(id).labelId(labelId).build();
            apartmentLabels.add(apartmentLabel);
        }
        apartmentLabelService.saveBatch(apartmentLabels);

        //保存公寓杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        for (Long feeValueId : feeValueIds) {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().apartmentId(id).feeValueId(feeValueId).build();
            apartmentFeeValues.add(apartmentFeeValue);
        }
        apartmentFeeValueService.saveBatch(apartmentFeeValues);

        //保存公寓图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(id);
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);

    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> list = apartmentInfoMapper.pageApartmentByQuery(page,queryVo);
        return list;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentInfo apartmentInfo = super.getById(id);
        if (apartmentInfo == null){
            return null;
        }
        List<FacilityInfo> facilityInfoList = apartmentInfoMapper.getFaclityInfoList(id);

        List<FeeValueVo> feeValueVoList = apartmentInfoMapper.getFeeValueList(id);

        List<LabelInfo> labelInfoList = apartmentInfoMapper.getLabelInfoList(id);

        List<GraphVo> graphVoList = apartmentInfoMapper.getGraphVoList(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        super.removeById(id);
        //删除公寓配套信息
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                .eq(ApartmentFacility::getApartmentId,id));
        //删除公寓标签信息
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                .eq(ApartmentLabel::getApartmentId,id));
        //删除公寓图片信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId,id));
        //删除公寓杂费信息
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                .eq(ApartmentFeeValue::getApartmentId,id));
    }
}




